/*   1:    */ package org.intellij.plugins.relaxNG.validation;
/*   2:    */ 
/*   3:    */

import com.intellij.ide.errorTreeView.NewErrorTreeViewPanel;
import com.intellij.ide.errorTreeView.NewErrorTreeViewPanel.ProcessController;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.XmlFile;
import com.intellij.xml.XmlElementDescriptor;
import com.thaiopensource.util.PropertyMapBuilder;
import com.thaiopensource.util.UriOrFile;
import com.thaiopensource.validate.ValidateProperty;
import com.thaiopensource.validate.ValidationDriver;
import com.thaiopensource.validate.auto.AutoSchemaReader;
import com.thaiopensource.validate.rng.CompactSchemaReader;
import com.thaiopensource.validate.rng.RngProperty;
import org.intellij.plugins.relaxNG.compact.RncFileType;
import org.intellij.plugins.relaxNG.model.descriptors.RngElementDescriptor;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import javax.swing.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.concurrent.Future;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */
/*  27:    */
/*  28:    */
/*  29:    */
/*  30:    */
/*  31:    */
/*  32:    */
/*  33:    */
/*  34:    */
/*  35:    */
/*  36:    */
/*  37:    */
/*  38:    */
/*  39:    */
/*  40:    */
/*  41:    */
/*  42:    */
/*  43:    */
/*  44:    */
/*  45:    */
/*  46:    */ 
/*  47:    */ public class ValidateAction
/*  48:    */   extends AnAction
/*  49:    */ {
/*  50:    */   private static final String a = "Validate RELAX NG";
/*  51: 65 */   private static final Key<NewErrorTreeViewPanel> b = Key.create("VALIDATING");
/*  52: 66 */   private static final Key<Boolean> c = Key.create("VALIDATION IN PROGRESS");
/*  53:    */   private final AnAction d;
/*  54:    */   
/*  55:    */   public ValidateAction(AnAction paramAnAction)
/*  56:    */   {
/*  57: 71 */     this.d = paramAnAction;
/*  58: 72 */     copyFrom(paramAnAction);
/*  59: 73 */     setEnabledInModalContext(paramAnAction.isEnabledInModalContext());
/*  60:    */   }
/*  61:    */   
/*  62:    */   public void actionPerformed(AnActionEvent paramAnActionEvent)
/*  63:    */   {
/*  64: 77 */     if (!a(paramAnActionEvent)) {
/*  65: 78 */       this.d.actionPerformed(paramAnActionEvent);
/*  66:    */     }
/*  67:    */   }
/*  68:    */   
/*  69:    */   public final void update(AnActionEvent paramAnActionEvent)
/*  70:    */   {
/*  71: 83 */     super.update(paramAnActionEvent);
/*  72: 84 */     this.d.update(paramAnActionEvent);
/*  73:    */     
/*  74: 86 */     VirtualFile localVirtualFile = (VirtualFile)paramAnActionEvent.getData(CommonDataKeys.VIRTUAL_FILE);
/*  75: 87 */     if ((localVirtualFile != null) && 
/*  76: 88 */       (localVirtualFile.getUserData(c) == Boolean.TRUE)) {
/*  77: 89 */       paramAnActionEvent.getPresentation().setEnabled(false);
/*  78:    */     }
/*  79:    */   }
/*  80:    */   
/*  81:    */   private boolean a(AnActionEvent paramAnActionEvent)
/*  82:    */   {
/*  83: 95 */     PsiFile localPsiFile = (PsiFile)paramAnActionEvent.getData(CommonDataKeys.PSI_FILE);
/*  84: 96 */     if (localPsiFile == null) {
/*  85: 97 */       return false;
/*  86:    */     }
/*  87: 99 */     Project localProject = (Project)paramAnActionEvent.getData(CommonDataKeys.PROJECT);
/*  88:100 */     if (localProject == null) {
/*  89:101 */       return false;
/*  90:    */     }
/*  91:104 */     RngElementDescriptor localRngElementDescriptor = a(localPsiFile);
/*  92:105 */     if (localRngElementDescriptor == null) {
/*  93:105 */       return false;
/*  94:    */     }
/*  95:107 */     PsiElement localPsiElement = localRngElementDescriptor.getDeclaration();
/*  96:108 */     XmlFile localXmlFile = (XmlFile)PsiTreeUtil.getParentOfType(localPsiElement, XmlFile.class);
/*  97:109 */     if (localXmlFile == null) {
/*  98:109 */       return false;
/*  99:    */     }
/* 100:111 */     VirtualFile localVirtualFile1 = localPsiFile.getVirtualFile();
/* 101:112 */     VirtualFile localVirtualFile2 = localXmlFile.getVirtualFile();
/* 102:113 */     if ((localVirtualFile1 == null) || (localVirtualFile2 == null)) {
/* 103:114 */       return true;
/* 104:    */     }
/* 105:117 */     a(localProject, localVirtualFile1, localVirtualFile2);
/* 106:    */     
/* 107:119 */     return true;
/* 108:    */   }
/* 109:    */   
/* 110:    */   private static void a(final Project paramProject, final VirtualFile paramVirtualFile1, final VirtualFile paramVirtualFile2)
/* 111:    */   {
/* 112:123 */     saveFiles(new VirtualFile[] { paramVirtualFile1, paramVirtualFile2 });
/* 113:    */     
/* 114:125 */     MessageViewHelper localMessageViewHelper = new MessageViewHelper(paramProject, "Validate RELAX NG", b);
/* 115:    */     
/* 116:127 */     localMessageViewHelper.openMessageView(new Runnable()
/* 117:    */     {
/* 118:    */       public void run()
/* 119:    */       {
/* 120:129 */         ValidateAction.a(this.val$project, paramVirtualFile1, paramVirtualFile2);
/* 121:    */       }
/* 122:132 */     });
/* 123:133 */     Future localFuture = ApplicationManager.getApplication().executeOnPooledThread(new Runnable()
/* 124:    */     {
/* 125:    */       public void run()
/* 126:    */       {
/* 127:135 */         ApplicationManager.getApplication().runReadAction(new Runnable()
/* 128:    */         {
/* 129:    */           public void run()
/* 130:    */           {
/* 131:137 */             MessageViewHelper tmp11_8 = ValidateAction.2.this.val$helper;tmp11_8.getClass();final MessageViewHelper.ErrorHandler localErrorHandler = new MessageViewHelper.ErrorHandler(tmp11_8);
/* 132:    */             
/* 133:139 */             ValidateAction.2.this.val$instanceFile.putUserData(ValidateAction.c, Boolean.TRUE);
/* 134:    */             try
/* 135:    */             {
/* 136:141 */               ValidateAction.a(ValidateAction.2.this.val$instanceFile, ValidateAction.2.this.val$schemaFile, localErrorHandler);
/* 137:    */             }
/* 138:    */             finally
/* 139:    */             {
/* 140:143 */               ValidateAction.2.this.val$instanceFile.putUserData(ValidateAction.c, null);
/* 141:    */             }
/* 142:146 */             SwingUtilities.invokeLater(new Runnable()
/* 143:    */             {
/* 144:    */               public void run()
/* 145:    */               {
/* 146:149 */                 if (!localErrorHandler.hadErrorOrWarning()) {
/* 147:150 */                   SwingUtilities.invokeLater(new Runnable()
/* 148:    */                   {
/* 149:    */                     public void run()
/* 150:    */                     {
/* 151:153 */                       ValidateAction.2.this.val$helper.close();
/* 152:154 */                       WindowManager.getInstance().getStatusBar(ValidateAction.2.this.val$project).setInfo("No errors detected");
/* 153:    */                     }
/* 154:    */                   });
/* 155:    */                 }
/* 156:    */               }
/* 157:    */             });
/* 158:    */           }
/* 159:    */         });
/* 160:    */       }
/* 161:166 */     });
/* 162:167 */     localMessageViewHelper.setProcessController(new NewErrorTreeViewPanel.ProcessController()
/* 163:    */     {
/* 164:    */       public void stopProcess()
/* 165:    */       {
/* 166:169 */         this.val$future.cancel(true);
/* 167:    */       }
/* 168:    */       
/* 169:    */       public boolean isProcessStopped()
/* 170:    */       {
/* 171:173 */         return this.val$future.isDone();
/* 172:    */       }
/* 173:    */     });
/* 174:    */   }
/* 175:    */   
/* 176:    */   private static void a(VirtualFile paramVirtualFile1, VirtualFile paramVirtualFile2, ErrorHandler paramErrorHandler)
/* 177:    */   {
/* 178:180 */     AutoSchemaReader localAutoSchemaReader = paramVirtualFile2.getFileType() == RncFileType.getInstance() ? CompactSchemaReader.getInstance() : new AutoSchemaReader();
/* 179:    */     
/* 180:    */ 
/* 181:    */ 
/* 182:184 */     PropertyMapBuilder localPropertyMapBuilder = new PropertyMapBuilder();
/* 183:185 */     ValidateProperty.ERROR_HANDLER.put(localPropertyMapBuilder, paramErrorHandler);
/* 184:    */     
/* 185:    */ 
/* 186:188 */     RngProperty.CHECK_ID_IDREF.add(localPropertyMapBuilder);
/* 187:    */     try
/* 188:    */     {
/* 189:191 */       String str1 = RngParser.reallyFixIDEAUrl(paramVirtualFile2.getUrl());
/* 190:    */       try
/* 191:    */       {
/* 192:193 */         ValidationDriver localValidationDriver = new ValidationDriver(localPropertyMapBuilder.toPropertyMap(), localAutoSchemaReader);
/* 193:194 */         InputSource localInputSource = ValidationDriver.uriOrFileInputSource(str1);
/* 194:195 */         localInputSource.setEncoding(paramVirtualFile2.getCharset().name());
/* 195:197 */         if (localValidationDriver.loadSchema(localInputSource))
/* 196:    */         {
/* 197:198 */           String str2 = RngParser.reallyFixIDEAUrl(paramVirtualFile1.getUrl());
/* 198:    */           try
/* 199:    */           {
/* 200:200 */             localValidationDriver.validate(ValidationDriver.uriOrFileInputSource(str2));
/* 201:    */           }
/* 202:    */           catch (IOException localIOException2)
/* 203:    */           {
/* 204:202 */             paramErrorHandler.fatalError(new SAXParseException(localIOException2.getMessage(), null, UriOrFile.fileToUri(str2), -1, -1, localIOException2));
/* 205:    */           }
/* 206:    */         }
/* 207:    */       }
/* 208:    */       catch (SAXParseException localSAXParseException)
/* 209:    */       {
/* 210:206 */         paramErrorHandler.fatalError(localSAXParseException);
/* 211:    */       }
/* 212:    */       catch (IOException localIOException1)
/* 213:    */       {
/* 214:208 */         paramErrorHandler.fatalError(new SAXParseException(localIOException1.getMessage(), null, UriOrFile.fileToUri(str1), -1, -1, localIOException1));
/* 215:    */       }
/* 216:    */     }
/* 217:    */     catch (SAXException localSAXException)
/* 218:    */     {
/* 219:212 */       Logger.getInstance(ValidateAction.class.getName()).error(localSAXException);
/* 220:    */     }
/* 221:    */     catch (MalformedURLException localMalformedURLException)
/* 222:    */     {
/* 223:214 */       Logger.getInstance(ValidateAction.class.getName()).error(localMalformedURLException);
/* 224:    */     }
/* 225:    */   }
/* 226:    */   
/* 227:    */   private static RngElementDescriptor a(PsiFile paramPsiFile)
/* 228:    */   {
/* 229:    */     try
/* 230:    */     {
/* 231:220 */       if ((paramPsiFile instanceof XmlFile))
/* 232:    */       {
/* 233:221 */         XmlElementDescriptor localXmlElementDescriptor = ((XmlFile)paramPsiFile).getDocument().getRootTag().getDescriptor();
/* 234:222 */         if ((localXmlElementDescriptor instanceof RngElementDescriptor)) {
/* 235:223 */           return (RngElementDescriptor)localXmlElementDescriptor;
/* 236:    */         }
/* 237:    */       }
/* 238:    */     }
/* 239:    */     catch (NullPointerException localNullPointerException) {}
/* 240:229 */     return null;
/* 241:    */   }
/* 242:    */   
/* 243:    */   public boolean displayTextInToolbar()
/* 244:    */   {
/* 245:233 */     return this.d.displayTextInToolbar();
/* 246:    */   }
/* 247:    */   
/* 248:    */   public void setDefaultIcon(boolean paramBoolean)
/* 249:    */   {
/* 250:237 */     this.d.setDefaultIcon(paramBoolean);
/* 251:    */   }
/* 252:    */   
/* 253:    */   public boolean isDefaultIcon()
/* 254:    */   {
/* 255:241 */     return this.d.isDefaultIcon();
/* 256:    */   }
/* 257:    */   
/* 258:    */   public void setInjectedContext(boolean paramBoolean)
/* 259:    */   {
/* 260:245 */     this.d.setInjectedContext(paramBoolean);
/* 261:    */   }
/* 262:    */   
/* 263:    */   public boolean isInInjectedContext()
/* 264:    */   {
/* 265:249 */     return this.d.isInInjectedContext();
/* 266:    */   }
/* 267:    */   
/* 268:    */   public static void saveFiles(VirtualFile... paramVarArgs)
/* 269:    */   {
/* 270:254 */     FileDocumentManager localFileDocumentManager = FileDocumentManager.getInstance();
/* 271:255 */     for (VirtualFile localVirtualFile : paramVarArgs)
/* 272:    */     {
/* 273:256 */       Document localDocument = localFileDocumentManager.getDocument(localVirtualFile);
/* 274:257 */       if (localDocument != null) {
/* 275:258 */         localFileDocumentManager.saveDocument(localDocument);
/* 276:    */       }
/* 277:    */     }
/* 278:    */   }
/* 279:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.intellij.plugins.relaxNG.validation.ValidateAction

 * JD-Core Version:    0.7.0.1

 */